package com.gf.message.mapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

import com.gf.base.type.MessageState;
import com.gf.base.type.MessageType;
import com.gf.common.util.DateFormater;
import com.gf.message.model.Message;

public class MessageMapper implements RowMapper<Message> {

	private final static String COLUME_ID = "id";
	private final static String COLUME_SENDERID = "senderId";
	private final static String COLUME_SENDERNAME = "senderName";
	private final static String COLUME_RECEIVEID = "receiveId";
	private final static String COLUME_RECEIVENAME = "receiveName";
	private final static String COLUME_DRAFT_RECEIVE_IDS = "draftReceiveIds";
	private final static String COLUME_DRAFT_RECEIVE_NAMES = "draftReceiveNames";
	private final static String COLUME_TITLE = "title";
	private final static String COLUME_CONTENT = "content";
	private final static String COLUME_CREATETIME = "createTime";
	private final static String COLUME_MESSAGETYPE = "messageType";
	private final static String COLUME_SENDER_STATE = "senderState";
	private final static String COLUME_RECEIVER_STATE = "receiverState";

	private static MessageMapper mapper = new MessageMapper();

	public static MessageMapper getMapper() {
		return mapper;
	}

	@Override
	public Message mapRow(ResultSet set, int row) throws SQLException {
		Message message = new Message();

		message.setId(set.getLong(COLUME_ID));
		message.setSenderId(set.getLong(COLUME_SENDERID));
		message.setSenderName(set.getString(COLUME_SENDERNAME));
		message.setReceiveId(set.getLong(COLUME_RECEIVEID));
		message.setReceiveName(set.getString(COLUME_RECEIVENAME));
		message.setDraftReceiveIds(set.getString(COLUME_DRAFT_RECEIVE_IDS));
		message.setDraftReceiveNames(set.getString(COLUME_DRAFT_RECEIVE_NAMES));
		message.setTitle(set.getString(COLUME_TITLE));
		message.setContent(set.getString(COLUME_CONTENT));
		message.setCreateTime(set.getTimestamp(COLUME_CREATETIME));
		message.setFormatCreateTime(DateFormater.changeToString(set.getTimestamp(COLUME_CREATETIME), "yyyy-MM-dd HH:mm"));
		message.setMessageType(MessageType.valueOf(set
				.getString(COLUME_MESSAGETYPE)));
		message.setSenderState(MessageState.valueOf(set
				.getString(COLUME_SENDER_STATE)));
		message.setReceiverState(MessageState.valueOf(set
				.getString(COLUME_RECEIVER_STATE)));

		return message;
	}

}
